﻿/**
 *    Copyright 2012 Yohan Courbe
 *
 *  Licensed under the Apache License, Version 2.0 (the "License") 
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *      
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

using System;
using MySql.Data.MySqlClient;
using System.Data;

namespace NetworkAnalyzer.SQL
{
    public sealed class MysqlDatabaseConnection
    {
        private readonly MySqlConnection _connection;
        public bool IsConnected { get; private set; }

        public static int TestConnection()
        {
            try
            {
                var mysqlCon = OpenConnection();
                mysqlCon.CloseConnection();

                return 0;
            }
            catch (MySqlException ex)
            {
                return ex.Number;
            }
        }

        public static int TestConnection(string hostname, string username, string password, string database)
        {
            var connectionString = "SERVER=" + hostname + ";" + "DATABASE=" + database + ";" + "UID=" + username + ";" + "PASSWORD=" + password + ";";

            try
            {
                var con = new MysqlDatabaseConnection(new MySqlConnection(connectionString));
                con.CloseConnection();

                return 0;
            }
            catch(MySqlException ex)
            {
                return ex.Number;
            }
        }

        public static MysqlDatabaseConnection OpenConnection()
        {
            var server = Properties.Settings.Default.db_hostname;
            var database = Properties.Settings.Default.db_database;
            var uid = Properties.Settings.Default.db_username;
            var password = Properties.Settings.Default.db_password;
            var connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";

            return new MysqlDatabaseConnection(new MySqlConnection(connectionString));
        }

        private MysqlDatabaseConnection(MySqlConnection connection)
        {
            this._connection = connection;

            connection.Open();
            IsConnected = true;
        }

        //Close connection
        public bool CloseConnection()
        {
            try
            {
                _connection.Close();
                return true;
            }
            catch (MySqlException ex)
            {
                Console.Error.WriteLine(ex.Message);
                return false;
            }
        }

        //Insert statement
        public void Query(string query)
        {
            //create command and assign the query and connection from the constructor
            var cmd = new MySqlCommand(query, _connection);


            //Execute command
            //try
            //{
                cmd.ExecuteNonQuery();
            //}
            //catch (Exception e)
            //{
            //    Console.WriteLine("MYSQL EXCEPTION: {0}\n{1}", e.Message, e.StackTrace);
            //}
        }

        public DataSet SelectQuery(string query)
        {
            var ds = new DataSet();
            var myAdapter = new MySqlDataAdapter {SelectCommand = new MySqlCommand(query, _connection)};

            try { myAdapter.Fill(ds); }
            catch (Exception)
            { }

            return ds;
        }
    }
}
